/**
 * Desenvolvido pela Fábrica de Software do CESUPA.
 * Todos os direitos reservados.
 */
package br.cesupa.fabsoft.nomedoprojeto.system.application.validators;

import br.cesupa.fabsoft.nomedoprojeto.system.application.facades.ApplicationFacade;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Breno Leite
 */
public class EMailValidator implements Validator {

    public static final String EMAIL_PATTERN = "^([\\w]+)(([-\\.][\\w]+)?)*@" +
            "((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))" +
            "([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";

    public void validate(final FacesContext context, final UIComponent component,
            final Object value) throws ValidatorException {

        if (!Pattern.matches(EMAIL_PATTERN, value.toString())) {
            throw new ValidatorException(
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    ApplicationFacade.getTranslationFacade().translateFacesMessage("InvalidEmail"),
                    null));
        }
    }
}
